def search_range(nums, target):
    """
    问题：在有序数组中找到目标值的起始和结束索引，不存在则返回[-1,-1]
    思路：两次二分查找，分别找左边界和右边界
    """

    def find_left():
        left, right = 0, len(nums) - 1
        res = -1
        while left <= right:
            mid = (left + right) // 2
            if nums[mid] == target:
                res = mid  # 记录可能的左边界，继续向左找
                right = mid - 1
            elif nums[mid] < target:
                left = mid + 1
            else:
                right = mid - 1
        return res

    def find_right():
        left, right = 0, len(nums) - 1
        res = -1
        while left <= right:
            mid = (left + right) // 2
            if nums[mid] == target:
                res = mid  # 记录可能的右边界，继续向右找
                left = mid + 1
            elif nums[mid] < target:
                left = mid + 1
            else:
                right = mid - 1
        return res

    left_idx = find_left()
    if left_idx == -1:
        return [-1, -1]
    return [left_idx, find_right()]


# 测试
print(search_range([5, 7, 7, 8, 8, 10], 8))  # 输出：[3,4]
print(search_range([5, 7, 7, 8, 8, 10], 6))  # 输出：[-1,-1]